package org.dromara.product.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.product.domain.bo.KyProductDetailsBo;
import org.dromara.product.domain.vo.KyProductDetailsVo;
import org.dromara.product.domain.KyProductDetails;
import org.dromara.product.mapper.KyProductDetailsMapper;
import org.dromara.product.service.IKyProductDetailsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品详情Service业务层处理
 *
 * @author kuyu
 * @date 2023-12-29
 */
@RequiredArgsConstructor
@Service
public class KyProductDetailsServiceImpl implements IKyProductDetailsService {

    private final KyProductDetailsMapper baseMapper;

    /**
     * 查询商品详情
     */
    @Override
    public KyProductDetailsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品详情列表
     */
    @Override
    public TableDataInfo<KyProductDetailsVo> queryPageList(KyProductDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KyProductDetails> lqw = buildQueryWrapper(bo);
        Page<KyProductDetailsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品详情列表
     */
    @Override
    public List<KyProductDetailsVo> queryList(KyProductDetailsBo bo) {
        LambdaQueryWrapper<KyProductDetails> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KyProductDetails> buildQueryWrapper(KyProductDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KyProductDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, KyProductDetails::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), KyProductDetails::getCode, bo.getCode());
        lqw.eq(bo.getWarehouseId() != null, KyProductDetails::getWarehouseId, bo.getWarehouseId());
        lqw.eq(bo.getZoneId() != null, KyProductDetails::getZoneId, bo.getZoneId());
        lqw.eq(bo.getNumber() != null, KyProductDetails::getNumber, bo.getNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), KyProductDetails::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getPrice() != null, KyProductDetails::getPrice, bo.getPrice());
        lqw.eq(bo.getUseStatus() != null, KyProductDetails::getUseStatus, bo.getUseStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getMgrUsr()), KyProductDetails::getMgrUsr, bo.getMgrUsr());
        lqw.eq(StringUtils.isNotBlank(bo.getInNo()), KyProductDetails::getInNo, bo.getInNo());
        return lqw;
    }

    /**
     * 新增商品详情
     */
    @Override
    public Boolean insertByBo(KyProductDetailsBo bo) {
        KyProductDetails add = MapstructUtils.convert(bo, KyProductDetails.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品详情
     */
    @Override
    public Boolean updateByBo(KyProductDetailsBo bo) {
        KyProductDetails update = MapstructUtils.convert(bo, KyProductDetails.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }



    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KyProductDetails entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
